Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Begin this project by creating a new Standard EXE project, placing a Picture Box and a Command Button on the form. Don’t worry about their exact position or size; we’ll take care of these specifications in code. Leave all the control properties at their default values except for the Command Button’s Caption property, which should be set to E&xit. Place the single statement End in the Command Button’s Click procedure. Save the project and the form (I used the name CENTER for both). Next, display the Form_Load event procedure and add the code shown in Listing 12.1. Listing 12.1 The Form_Load event procedure.
    Private Sub Form_Load()

    ‘ Make the form width and height equal to
    ‘ half the screen dimensions.
    Form1.Width = Screen.Width / 2
    Form1.Height = Screen.Height / 2

    ‘ Center the form on the screen.
    Form1.Left = (Screen.Width - Form1.Width) / 2
    Form1.TOP = (Screen.Height - Form1.Height) / 2

    End Sub
When you start the program, the form displays centered on the screen with width and height equal to half the screen dimensions. The controls, however, remain in the positions they were given during program design. Setting the control positions is the next task. To set the size and positions of the controls, I use an approach like the one used for the form: Specify each control’s size and position in terms of its container’s size. Where should we place this code? We want the controls to be positioned whenever the form size changes, as well as when the form is first displayed. The ideal place for this code is the Form_Resize event procedure, which is called when the form first displays and any time its size is changed (either by the user or in code). I won’t bother explaining the code in this procedure (shown in Listing 12.2); I’m sure you’ll be able to understand how it works from the comments. Don’t forget to save the project as you work on it. Listing 12.2 The Form_Resize event procedure.
    Private Sub Form_Resize()

    ‘ Make the Picture Box size equal to 90% of the form
    ‘ width and 70% of its height.
    Picture1.Width = Form1.ScaleWidth * 0.9
    Picture1.Height = Form1.ScaleHeight * 0.7

    ‘ Center the Picture Box left-to-right.
    Picture1.Left = (Form1.ScaleWidth - Picture1.Width) / 2

    ‘ Make the distance between the top of the Picture Box and
    ‘ the form the same as the distance at the sides.
    Picture1.TOP = Picture1.Left

    ‘ Make the Command Button width 20% of the form width.
    Command1.Width = Form1.ScaleWidth * 0.2

    ‘ Make the Command Button height one third of the distance between the
    ‘ bottom of the Picture Box and the edge of the form.
    Command1.Height = (Form1.ScaleHeight - Picture1.ScaleHeight) / 3

    ‘ Center the Command Button left-to-right.
    Command1.Left = (Form1.ScaleWidth - Command1.Width) / 2

    ‘ Center the Command Button vertically in the space between the
    ‘ Picture Box and the edge of the form.
    Command1.TOP = Form1.ScaleHeight - 0.66 * _
        (Form1.ScaleHeight - (Picture1.TOP + Picture1.Height))

    End Sub
Once the Form_Resize procedure is complete, run the program again. You’ll see that the Picture Box and Command Button are precisely positioned in the form, as shown in Figure 12.2. If you change the window size by dragging a border, you’ll see that the controls maintain their relative sizes and positions. TIP: Using Fixed-Sized Controls Not all windows need to be resizable. In fact, dialog boxes that contain an array of Text Boxes, Option Buttons, and other controls are best left at a fixed size. You can accomplish this by setting the form’s BorderStyle property to: 1 - Fixed Single, 3 - Fixed Dialog, or 4 -Fixed Toolwindow. Then you can design the form, placing the various controls in precise positions for the results you want, without worrying about the user messing things up by resizing the dialog box. You could also use the techniques presented here to adapt each control’s size and position when the dialog box is resized. In my experience, that’s a lot more work than it’s worth. Figure 12.2 Automatically setting the size and position of controls. #### Who’s On First? Before continuing our discussion of Visual Basic’s coordinate system, I think it’s wise to take a brief detour to explain the events that occur when a form is being loaded and displayed. This is an area that confuses many programmers, so if you get it straight now, you won’t have to worry about it later. Each form in a Visual Basic project exists in its own disk file. Showing a form on screen requires two steps: 1. Loading the form from disk into memory 2. Displaying the form In a single-form project, the form is loaded and displayed when the program starts. For a multiple-form project, the designated startup form is loaded and displayed when the program starts. Other forms are loaded and displayed under program control. You can use the Load statement to load a form without displaying it:
    Load formname
A form will also be loaded if the program refers to any of its properties. Loading a form and displaying it are two different things, however. The Show method, shown here
    formname.Show
is used to display a form. If the form has not already been loaded, Show loads it automatically. Rarely will you need to use the Load statement. I have found two instances, however, in which Load is useful: • If you want the form’s Load event procedure to execute without the form being displayed, use the Load statement. • If the form is complex (that is, if it has many controls and/or performs complex processing in its Load event procedure), use the Load statement to load the form before you need to display it. Complex forms display more quickly if they’ve been loaded ahead of time. You can unload a form you no longer need if you want to free memory or to reset all of the form’s properties to their original values. To unload a form, use the Unload statement:
    Unload formname
If you want to remove a form from the screen without unloading it, use the Hide method:
    formname.Hide
Forms also have a Paint event whose occurrence is related to a need to redraw some or all of the form. Figure 12.3 summarizes the relationships among the various methods, statements, and user actions that act on forms and the events that are triggered as a result. You’ll see how the Paint event is used later in this chapter.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.